<?php

define('IN_ECS', true);
require('../includes/init.php');
require_once(ROOT_PATH . '/includes/lib_order.php');
require_once(ROOT_PATH . '/includes/lib_wangcang.php');
$smarty->template_dir  = ROOT_PATH . '/wangcang/tpl';

if(empty($_REQUEST['act'])) $_REQUEST['act'] = '';

// TODO : 登录部分准备拿出去做，到时候把以下操作一起挪过去
if ($_REQUEST['act'] == 'captcha')
{
    include_once(ROOT_PATH . 'includes/cls_captcha.php');

    $img = new captcha('../data/captcha/');
    @ob_end_clean(); //清除之前出现的多余输入
    $img->generate_image();

    exit;
}
if($_REQUEST['act'] == 'signins'){
    include_once(ROOT_PATH . 'includes/cls_captcha.php');

    /* 检查验证码是否正确 */
    $validator = new captcha();
    if (empty($_POST['captcha']) || !$validator->check_word($_POST['captcha']))
    {
        sys_msg('验证码错误', 1);
    }
    $_POST['username'] = isset($_POST['username']) ? trim($_POST['username']) : '';
    $_POST['password'] = isset($_POST['password']) ? trim($_POST['password']) : '';

    if($_POST['username'] != 'wangcang' || $_POST['password'] != 'wangcang123'){
        sys_msg('账号密码错误', 1);
    }else{
        $_SESSION['wcLogin'] = true;
        header('location:index.php');
        exit;
    }
}
if(empty($_SESSION['wcLogin'])){
    $smarty->display('login.html');
}
elseif($_REQUEST['act'] == 'print'){
    if(!empty($_REQUEST['iscs_order_no'])){
        $data = vgettradeQuery2(array($_REQUEST['iscs_order_no']), $v_data);
        if(!empty($data['0']['data']['trades']['0']['order_no'])){
            $orderSn = $data['0']['data']['trades']['0']['order_no'];
        }else{
            sys_msg('没有此订单号的信息', 1);
        }
    }elseif(empty($_REQUEST['orderSn'])){
        sys_msg('请输入订单号查询', 1);
    }else{
        $orderSn = intval($_REQUEST['orderSn']);
    }
    $order = order_info(0, $orderSn);



    if ($order['agency_id'] == 0)
    {
        sys_msg('此订单不属于珠海网仓管理');
    }
    /* 格式化金额 */
    if ($order['order_amount'] < 0)
    {
        $order['money_refund']          = abs($order['order_amount']);
        $order['formated_money_refund'] = price_format(abs($order['order_amount']));
    }

    /* 其他处理 */
    $order['order_time']    = local_date($_CFG['time_format'], $order['add_time']);
    $weight_price = order_weight_price($order['order_id']);
    $order['total_weight'] = $weight_price['formated_weight'];
    $order['region'] = selectaddress($order['province']) . '/' . selectaddress($order['city']) . '/' . selectaddress($order['district']);
    $data = vgettradeQuery(array($order['order_sn']), $v_data);
    if(!empty($data['0']['data']['trades']['0']['iscs_order_no'])){
        $order['order_sns'] = $data['0']['data']['trades']['0']['iscs_order_no'];
    }
    $smarty->assign('order', $order);
    /* 取得订单商品及货品 */
    $goods_list = array();
    $goods_attr = array();
    $goods_list = orderGoods($order['order_id']);
    $goods_list=bubble_sort($goods_list,'isBarcode', 'DESC');

    foreach ($goods_list as $key => $value) {
        if($value['goods_id']){
            $goods_count = $goods_count+$value['goods_number'];
        }
    }

    $attr = array();
    $arr  = array();
    $smarty->assign('goods_count', $goods_count);
    $smarty->assign('goods_attr', $attr);
    $smarty->assign('goods_list', $goods_list);
    $smarty->display('print.html');
}
else{
    /* 获取未发货列表 */
    $sql = "SELECT * FROM " . $ecs->table('order_info') . " WHERE order_status = 1 AND wangcangstatus >= 100 AND wangcangstatus < 420 AND agency_id = 3";
    $orderList = $db->getAll($sql);
    foreach ($orderList as $key => $order) {
        $order['region'] = selectaddress($order['province']) . '/' . selectaddress($order['city']) . '/' . selectaddress($order['district']);
        $order['add_time'] = date('Y-m-d H:i:s', $order['add_time']);
        $data = vgettradeQuery(array($order['order_sn']), $v_data);
        if(!empty($data['0']['data']['trades']['0']['iscs_order_no'])){
            $order['order_sns'] = $data['0']['data']['trades']['0']['iscs_order_no'];
        }
        $orderList[$key] = $order;
    }
    $smarty->assign('orderList', $orderList);
    $smarty->display('index.html');
}

/*
* 冒泡排序
* $arr 需排序的数组
* $val 排序的数组键
* $type 排序方式 ASC或DESC
*/
function bubble_sort($arr,$val,$type){
    $arr=array_values($arr);
    for($i=0;$i<count($arr)-1;$i++ ){      
        for($j=0;$j<count($arr)-1-$i;$j++){
            if($type=='DESC'){
                if(strcasecmp($arr[$j]["$val"],$arr[$j+1]["$val"]) < 0 || $arr[$j]["$val"] < $arr[$j+1]["$val"]){
                    $tmp = $arr[$j];
                    $arr[$j] = $arr[$j+1];
                    $arr[$j+1] = $tmp;
                }
            }else{
                if(strcasecmp($arr[$j]["$val"],$arr[$j+1]["$val"]) > 0 || $arr[$j]["$val"] > $arr[$j+1]["$val"]){
                    $tmp = $arr[$j];
                    $arr[$j] = $arr[$j+1];
                    $arr[$j+1] = $tmp;
                }
            }
        }
    }
    return $arr;
}

/**
 * 系统提示信息
 *
 * @access      public
 * @param       string      msg_detail      消息内容
 * @param       int         msg_type        消息类型， 0消息，1错误，2询问
 * @param       array       links           可选的链接
 * @param       boolen      $auto_redirect  是否需要自动跳转
 * @return      void
 */
function sys_msg($msg_detail, $msg_type = 0, $links = array(), $auto_redirect = true)
{
    if (count($links) == 0)
    {
        $links[0]['text'] = '返回上一页';
        $links[0]['href'] = 'javascript:history.go(-1)';
    }


    $GLOBALS['smarty']->assign('ur_here',     '错误信息');
    $GLOBALS['smarty']->assign('msg_detail',  $msg_detail);
    $GLOBALS['smarty']->assign('msg_type',    $msg_type);
    $GLOBALS['smarty']->assign('links',       $links);
    $GLOBALS['smarty']->assign('default_url', $links[0]['href']);
    $GLOBALS['smarty']->assign('auto_redirect', $auto_redirect);

    $GLOBALS['smarty']->display('message.html');

    exit;
}